﻿using System;
using System.Collections.Generic;
using System.Linq;

namespace ud_Article.Business
{
    public class DBArticleAttachment
    {
        readonly LinqArticle db = new LinqArticle();
        public ud_ArticleAttachment GetOne(int fileID)
        {
            var result = from p in db.ud_ArticleAttachments
                         where p.FileID.Equals(fileID)
                         select p;
            return result.FirstOrDefault();
        }

        public List<ud_ArticleAttachment> GetAll(int moduleID)
        {
            var result = from p in db.ud_ArticleAttachments
                         where p.ModuleID.Equals(moduleID)
                         orderby p.LastModified descending
                         select p;
            return result.ToList();
        }

        public List<ud_ArticleAttachment> GetAll(string serverFileName)
        {
            var result = from p in db.ud_ArticleAttachments
                         where p.ServerFileName == serverFileName
                         orderby p.LastModified descending
                         select p;
            return result.ToList();
        }

        public void IncreaseDownloadCount(int fileID)
        {
            var result = GetOne(fileID);
            result.DownloadCount++;
            db.SubmitChanges();
        }

        public object GetByItemID(int itemID)
        {
            //DBArticleItemAttachment repository = new DBArticleItemAttachment();
            //List<ud_ArticleItemAttachment> listItem = repository.GetAllByItemID(itemID);
            //{
            //    var item = from p in db.
            //}
            //List<ud_ArticleAttachment> result = new List<ud_ArticleAttachment>();
            
            var result = from p in db.ud_ArticleItemAttachments
                         from q in db.ud_ArticleAttachments
                         where p.FileID == q.FileID && p.ItemID == itemID
                         select new {p, q};
            return result.ToList();
        }

        public bool Delete(int fileID)
        {
            bool flag = false;
            ud_ArticleAttachment item = GetOne(fileID);
            db.ud_ArticleAttachments.DeleteOnSubmit(item);
            try
            {
                db.SubmitChanges();
                flag = true;
            }
            catch
            { }
            return flag;
        }

        public bool Delete(string serverFileName)
        {
            bool flag = false;
            List<ud_ArticleAttachment> listItem = GetAll(serverFileName);
            db.ud_ArticleAttachments.DeleteAllOnSubmit(listItem);
            try
            {
                db.SubmitChanges();
                flag = true;
            }
            catch
            { }
            return flag;
        }

        public bool Create(int moduleID, string fileName, string serverFileName, int sizeInKB, out int fileID)
        {
            bool flag = false;

            ud_ArticleAttachment item = new ud_ArticleAttachment
                                            {
                                                ModuleID = moduleID,
                                                FileName = fileName,
                                                ServerFileName = serverFileName,
                                                SizeInKB = sizeInKB,
                                                DownloadCount = 0,
                                                LastModified = DateTime.UtcNow
                                            };
            db.ud_ArticleAttachments.InsertOnSubmit(item);
            try
            {
                db.SubmitChanges();
                flag = true;
            }
            catch { }
            fileID = item.FileID;
            return flag;
        }

        public bool Update(int categoryID, string fileName, string serverFileName, int sizeInKB)
        {
            bool flag = false;

            ud_ArticleAttachment item = GetOne(categoryID);
            if (item != null)
            {
                item.FileName = fileName;
                item.ServerFileName = serverFileName;
                item.SizeInKB = sizeInKB;
                item.LastModified = DateTime.UtcNow;
                try
                {
                    db.SubmitChanges();
                    flag = true;
                }
                catch { }
            }
            return flag;
        }

        public void DeleteByModule(int moduleID)
        {
            var result = from p in db.ud_ArticleAttachments
                         where p.ModuleID.Equals(moduleID)
                         select p;
            db.ud_ArticleAttachments.DeleteAllOnSubmit(result);
            var resultItemAttachment = from p in db.ud_ArticleItemAttachments
                         where p.ModuleID.Equals(moduleID)
                         select p;
            db.ud_ArticleAttachments.DeleteAllOnSubmit(result);
            db.ud_ArticleItemAttachments.DeleteAllOnSubmit(resultItemAttachment);
            db.SubmitChanges();
        }

        public ud_ArticleItemAttachment GetOneItemAttachment(int ID)
        {
            var result = from p in db.ud_ArticleItemAttachments
                         where p.ID.Equals(ID)
                         select p;
            return result.FirstOrDefault();
        }

        public List<ud_ArticleItemAttachment> GetAllItemAttachment(int moduleID)
        {
            var result = from p in db.ud_ArticleItemAttachments
                         where p.ModuleID.Equals(moduleID)
                         select p;
            return result.ToList();
        }

        public List<ud_ArticleItemAttachment> GetAllItemAttachment(string serverFileName)
        {
            var result = from p in db.ud_ArticleItemAttachments
                         join q in db.ud_ArticleAttachments on p.FileID equals q.FileID
                         where q.ServerFileName == serverFileName
                         select p;
            return result.ToList();
        }

        public List<ud_ArticleItemAttachment> GetAllItemAttachmentByItemID(int itemID)
        {
            var result = from p in db.ud_ArticleItemAttachments
                         where p.ItemID.Equals(itemID)
                         select p;
            return result.ToList();
        }

        public List<ud_ArticleItemAttachment> GetAllItemAttachmentByFileID(int fileID)
        {
            var result = from p in db.ud_ArticleItemAttachments
                         where p.FileID.Equals(fileID)
                         select p;
            return result.ToList();
        }

        public bool DeleteItemAttachment(int ID)
        {
            bool flag = false;
            ud_ArticleItemAttachment item = GetOneItemAttachment(ID);
            db.ud_ArticleItemAttachments.DeleteOnSubmit(item);
            try
            {
                db.SubmitChanges();
                flag = true;
            }
            catch
            { }
            return flag;
        }

        public bool DeleteItemAttachment(string serverFileName)
        {
            bool flag = false;
            List<ud_ArticleItemAttachment> listItem = GetAllItemAttachment(serverFileName);
            db.ud_ArticleItemAttachments.DeleteAllOnSubmit(listItem);
            try
            {
                db.SubmitChanges();
                flag = true;
            }
            catch
            { }
            return flag;
        }

        public bool DeleteItemAttachmentByItemID(int itemID)
        {
            bool flag = false;
            List<ud_ArticleItemAttachment> listItem = GetAllItemAttachmentByItemID(itemID);
            List<ud_ArticleAttachment> listFile = new List<ud_ArticleAttachment>();
            foreach (var udArticleItemAttachment in listItem)
            {
                listFile.Add(GetOne(udArticleItemAttachment.FileID));
            }
            db.ud_ArticleAttachments.DeleteAllOnSubmit(listFile);
            db.ud_ArticleItemAttachments.DeleteAllOnSubmit(listItem);
            try
            {
                db.SubmitChanges();
                flag = true;
            }
            catch
            { }
            return flag;
        }

        public bool CreateItemAttachment(int moduleID, int itemID, int fileID)
        {
            bool flag = false;

            ud_ArticleItemAttachment item = new ud_ArticleItemAttachment
            {
                ModuleID = moduleID,
                ItemID = itemID,
                FileID = fileID
            };
            db.ud_ArticleItemAttachments.InsertOnSubmit(item);
            try
            {
                db.SubmitChanges();
                flag = true;
            }
            catch { }
            return flag;
        }

        public bool UpdateItemAttachment(int ID, int itemID, int fileID)
        {
            bool flag = false;

            ud_ArticleItemAttachment item = GetOneItemAttachment(ID);
            if (item != null)
            {
                item.ItemID = itemID;
                item.FileID = fileID;
                try
                {
                    db.SubmitChanges();
                    flag = true;
                }
                catch { }
            }
            return flag;
        }

    }
}
